<?php
/**
 * Created by PhpStorm.
 * User: 冯轲
 * Date: 2017-12-4
 * Time: 8:54
 */
namespace app\home\controller;

use app\admin\model\User;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use ucpaas\Ucpaas;

class Cart extends Controller
{
    //购物车页面
    public function getIndex()
    {
        //计算出来当前用户的购物车的商品
        $uid = (Session::get('userinfo'))['user_id'];
        $cart = Db::name('cart_goods')
            ->alias('cg')
            ->join('cart c', 'cg.cart_id=c.id')
            ->join('goods g', 'cg.goods_id=g.id')
            ->join('models m','cg.goods_model=m.id')
            ->where('c.uid', $uid)
            ->select();
        $this->assign('data', $cart);
        return $this->fetch('cart/index');
    }

    //添加到购物车
    public function getadd($id)
    {
        $res=input();
        if(empty($res['model'])){
            $data = [
                'status' => 2,
                'msg' => '请选择型号',
            ];
            return $data;
        }
        //添加到购物车就需要选取型号
        $model=$res['model'];
        //得到了商品id，还有存在session中的用户id
        $uid= (Session::get('userinfo'))['user_id'];
        if(empty($uid)){
            $data = [
                'status' => 3,
                'msg' => '请登录',
            ];
            return $data;
        }
        //用户建一个购物车,得到购物车id
        $ins = [
            'uid' => $uid,
        ];
        //先查询是否存在购物车,如果存在就进行商品的插入，不添加数据
        if ($cart_id = Db::name('cart')->where('uid', $uid)->value('id')) {
            //查询商品是否已经添加过
            $goods = Db::name('cart_goods')->where('goods_id', $id)->find();
            $carts = [
                'goods_id' => $id,
                'cart_id' => $cart_id,
                'cart_num' => 1,
                'goods_model'=>$model,
            ];
            if (!$goods) {
                $res = Db::name('cart_goods')->insert($carts);
                if ($res) {
                    $data = [
                        'status' => 0,
                        'msg' => '商品已加至购物车',
                    ];
                } else {
                    $data = [
                        'status' => 1,
                        'msg' => '商品添加购物车失败'
                    ];
                }
                return json($data);
            }else{

                //判断商品和型号都相同，相同则增加数量
                $cart_num = Db::name('cart_goods')
                    ->where('goods_id', $id)
                    ->where('goods_model',$model)
                    ->value('cart_num');

                if($cart_num){
                    //表示商品和相同型号已经存在
                    $cart_num += 1;
                    $num = [
                        'cart_num' => $cart_num,
                    ];
                    //进行商品的增加
                    $res = Db::name('cart_goods')
                        ->where('goods_id', $id)
                        ->where('goods_model',$model)
                        ->update($num);
                    if ($res) {
                        $data = [
                            'status' => 0,
                            'msg' => '商品已增加',
                        ];
                    } else {
                        $data = [
                            'status' => 1,
                            'ms`g' => '商品增加失败'
                        ];
                    }
                }else{
                    //表示商品存在，型号不存在
                    $res = Db::name('cart_goods')->insert($carts);
                    if ($res) {
                        $data = [
                            'status' => 0,
                            'msg' => '商品已加至购物车',
                        ];
                    } else {
                        $data = [
                            'status' => 1,
                            'msg' => '商品添加购物车失败'
                        ];
                    }
                    return json($data);
                }

            }

        } else {
            Db::name('cart')->insert($ins);
            $cart_id = Db::name('cart')->getLastInsID();
            //通过购物车id创建购物车与商品的关联表
            $carts = [
                'goods_id' => $id,
                'cart_id' => $cart_id,
                'cart_num' => 1,
                'goods_model' =>$model ,
            ];

            $res = Db::name('cart_goods')->insert($carts);
            if ($res) {
                $data = [
                    'status' => 0,
                    'msg' => '购物车添加成功',
                ];
            } else {
                $data = [
                    'status' => 1,
                    'msg' => '购物车添加失败'
                ];
            }
        }
        return json($data);
    }

    //购物车商品数量变化
    public function postCart_num()
    {
        $cart = input('post.');
        $id = $cart['id'];
        $num = $cart['num'];
        //写入数据库
        if (Db::name('cart_goods')->where('goods_id', $id)->update(['cart_num' => $num])) {
            $data = [
                'status' => 0,
                'msg' => '修改成功',
            ];
        } else {
            $data = [
                'status' => 1,
                'msg' => '修改失败',
            ];
        }
        return json($data);
    }

    //购物车删除商品
    public function postCart_delete($id,$goods_model)
    {

        $re=Db::name('cart_goods')
            ->where('goods_id',$id)
            ->where('goods_model',$goods_model)
            ->delete();
        if($re){
            //返回数据，进行判断是否添加成功
            $data=[
                'status' => 0,
                'msg' => '删除成功',
            ];
        }else{
            $data=[
                'status' => 1,
                'msg' => '删除失败',
            ];
        };
        return json($data);
    }


}